using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace sales.Controls
{
    public partial class prodTemplateInfo : common.controls.baseUserControl
    {
        public prodTemplateInfo()
        {
            InitializeComponent();
            itemCodeEd.myOnFind += new common.controls.baseTextBox.onFind(DoFindItem);

            itemNameEd.BackColor = common.Settings.sysColorDisableBG;
            itemNameEd.ForeColor = common.Settings.sysColorDisableFG;

            itemNameEd.BackColor = common.Settings.sysColorDisableBG;
            itemNameEd.ForeColor = common.Settings.sysColorDisableFG;

            itemUnitEd.BackColor = common.Settings.sysColorDisableBG;
            itemUnitEd.ForeColor = common.Settings.sysColorDisableFG;

            parentItemCodeEd.BackColor = common.Settings.sysColorDisableBG;
            parentItemCodeEd.ForeColor = common.Settings.sysColorDisableFG;

            parentItemNameEd.BackColor = common.Settings.sysColorDisableBG;
            parentItemNameEd.ForeColor = common.Settings.sysColorDisableFG;
        }
        public void GetData(data.salesDS.ProdTemplateRow row)
        {
            row.itemCode = itemCodeEd.Text.Trim();
            parentItemCodeEd.Text = parentItemCodeEd.Text.Trim();
            if (parentItemCodeEd.Text=="") row.SetitemCodeGroupNull();
            else row.itemCodeGroup = parentItemCodeEd.Text;
            row.qty = itemQtyEd.Value;
            row.weight1 = weightEd1.Value;
            row.weight2 = weightEd2.Value;
        }
        public void SetData(data.salesDS.ProdTemplateRow row)
        {
            data.masterDS.ItemRow itemRow;
            itemCodeEd.Text = ""; itemNameEd.Text = ""; itemUnitEd.Text = "";
            if (row.itemCode != null && row.itemCode.Trim() != "")
            {
                itemRow = DataAccess.DataCache.FindITEM(row.itemCode);
                if (itemRow != null)
                {
                    itemCodeEd.Text = itemRow.itemCode;
                    itemNameEd.Text = itemRow.name;
                    itemUnitEd.Text = itemRow.unit;
                }
            }
            itemCodeEd.Text = row.itemCode.Trim();
            itemQtyEd.Value = row.qty;
            weightEd1.Value = (row.Isweight1Null() ? 0 : row.weight1);
            weightEd2.Value = (row.Isweight2Null() ? 0 : row.weight2);
            parentItemCodeEd.Text = ""; parentItemNameEd.Text = "";
            if (!row.IsitemCodeGroupNull() && row.itemCodeGroup.Trim() != "")
            {
                itemRow = DataAccess.DataCache.FindITEM(row.itemCodeGroup);
                if (itemRow != null)
                {
                    parentItemCodeEd.Text = itemRow.itemCode;
                    parentItemNameEd.Text = itemRow.name;
                }
            }
        }
        
        public void LockEdit(bool lockState)
        {
            itemCodeEd.ReadOnly = lockState;
            itemQtyEd.ReadOnly = lockState;
            weightEd1.ReadOnly = lockState;
            weightEd2.ReadOnly = lockState;
        }
        public void SetFocus()
        {
            this.Focus();
            itemCodeEd.Focus();
        }
        
        private void DoFindItem(object sender)
        {
            string code = itemCodeEd.Text.Trim().Replace(CommonTypes.Settings.sysNewData, "");
            data.masterDS.ItemRow row = DataAccess.DataCache.FindITEM(code);
            if (row != null)
            {
                itemCodeEd.Text = row.itemCode;
                itemNameEd.Text = row.name;
                itemUnitEd.Text = row.unit;
            }
            else
            {
                this.itemCodeEd.Text = "";
                itemNameEd.Text = "";
                itemUnitEd.Text = "";
            }
        }
    }
}
